package string.nowcoder.medium;

import java.util.Scanner;

/**
 * @author bruin_du
 * @description 扑克牌大小
 * https://www.nowcoder.com/practice/0a92c75f5d6b4db28fcfa3e65e5c9b3f?
 * tpId=49&&tqId=29277&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking
 * @date 2022/5/13 16:50
 **/
//方法一
public class ComparePlayingCard {
    //保存所有牌,并按照大小从前往后排列
    static String[] allCard = new String[]{"3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"};
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            //将字符串拆成两手牌
            String[] strings = str.split("-");
            //把手牌拆成单张牌,好为了后面比较
            String[] stringsL = strings[0].split(" ");
            String[] stringsR = strings[1].split(" ");
            //保存手牌的张数
            int lengthL = stringsL.length;
            int lengthR = stringsR.length;
            //用于判断是否有两张大小王
            boolean flagNumOf2L = false;
            boolean flagNumOf2R = false;
            //根据首字母选择大的字符串
            int indexL = findIndex(stringsL[0]);
            int indexR = findIndex(stringsR[0]);
            int condition = indexL - indexR;
            String printStr = null;
            if(condition > 0)
                printStr = strings[0];
            else
                printStr = strings[1];
            //如果两幅手牌中有炸,就会被if拦截下
            if(((stringsL[0].length() == 5 && stringsL.length == 2 && (flagNumOf2L = true)) ||
                    (stringsR[0].length() == 5 && stringsR.length == 2 && (flagNumOf2R = true))) ||
                    lengthL == 4 || lengthR == 4 ){
                //左手牌有王炸
                if(flagNumOf2L){
                    System.out.println(strings[0]);
                }
                //右手牌有王炸
                else if(flagNumOf2R){
                    System.out.println(strings[1]);
                }
                //没有王炸,两手牌都是四张的炸
                else if(lengthL == 4 && lengthR == 4){
                    System.out.println(printStr);
                }
                //只有一边有炸
                else if(lengthL == 4){
                    System.out.println(strings[0]);
                }else {
                    System.out.println(strings[1]);
                }
            }
            //这个if里面没有炸,两个字符串数组大小一样,说明类型匹配
            //之前比较过了大小,直接输出
            else if(lengthL == lengthR){
                System.out.println(printStr);
            }
            //类型不匹配,不能比较
            else{
                System.out.println("ERROR");
            }
        }
    }
    //按照定义的字符串数组找到对应的索引
    //可以根据索引比较手牌的大小
    public static int findIndex(String str){
        for (int i = 0; i < allCard.length; i++) {
            if(str.equals(allCard[i]))
                return i;
        }
        return -1;
    }
}

//方法二
//处理好优先级
class ComparePlayingCard2{
    static String[] allCard = new String[]{"3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"};
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
            String inputStr = in.nextLine();
            String[] strings = inputStr.split("-");
            String[] stringsL = strings[0].split(" ");
            String[] stringsR = strings[1].split(" ");
            int lengthL = stringsL.length;
            int lemgthR = stringsR.length;
            if(strings[0].equals("joker JOKER") || strings[1].equals("joker JOKER")){
                System.out.println("joker JOKER");
            }else if(lengthL == lemgthR){
                if(findIndex(stringsL[0]) > findIndex(stringsR[0]))
                    System.out.println(strings[0]);
                else
                    System.out.println(strings[1]);
            }else if(lengthL == 4){
                System.out.println(strings[0]);
            }else if(lemgthR == 4){
                System.out.println(strings[1]);
            }else{
                System.out.println("ERROR");
            }
        }
    }
    public static int findIndex(String str){
        for (int i = 0; i < allCard.length; i++) {
            if(str.equals(allCard[i]))
                return i;
        }
        return -1;
    }
}
